#include "aky_program/aky_com_include.h"
static void my_event_cb(lv_obj_t * obj, lv_event_t event);

static lv_obj_t *tc_area_next;          //
static lv_obj_t *tc_area_skip;          //
static lv_obj_t *ta_name;          //
static lv_obj_t *ta_gender;          //
static lv_obj_t *ta_age;          //
static lv_obj_t *ta_phone_num;          //


/*
 * 界面回调函数
 */
static void screen004_on_timer(void *user_data)
{
    /* 界面定时器回调函数 */

    screen_flush_pro();
}

static void screen004_start(synwit_id_t screen_id)
{
    /* 在界面被显示给用户前，这个接口会被调用。 */

	lv_obj_set_style_local_border_width(synwit_ui_find_lv_obj(SCREEN004), LV_OBJ_PART_MAIN, LV_STATE_FOCUSED, 0);
	lv_obj_set_style_local_outline_width(synwit_ui_find_lv_obj(SCREEN004), LV_OBJ_PART_MAIN, LV_STATE_FOCUSED, 0);
	lv_obj_set_style_local_border_width(synwit_ui_find_lv_obj(SCREEN004), LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, 0);
	lv_obj_set_style_local_outline_width(synwit_ui_find_lv_obj(SCREEN004), LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, 0);
	lv_font_t* font_key_board = synwit_ui_font_load("S:spi:wqyMicroHeiMono_40.hmf", false);
    if (font_key_board != NULL)
    {
    	lv_obj_set_style_local_text_font(synwit_ui_find_lv_obj(WIDGET_TEXTAREA_1), LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, font_key_board);	
    	lv_obj_set_style_local_text_font(synwit_ui_find_lv_obj(WIDGET_TEXTAREA_2), LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, font_key_board);	
    	lv_obj_set_style_local_text_font(synwit_ui_find_lv_obj(WIDGET_TEXTAREA_3), LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, font_key_board);	
    	lv_obj_set_style_local_text_font(synwit_ui_find_lv_obj(WIDGET_TEXTAREA_4), LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, font_key_board);	
        /* code */
    }
    


	lv_demo_chineseinput2();

    ta_name = synwit_ui_find_lv_obj(WIDGET_TEXTAREA_1);
    ta_gender = synwit_ui_find_lv_obj(WIDGET_TEXTAREA_2);
    ta_age = synwit_ui_find_lv_obj(WIDGET_TEXTAREA_3);
    ta_phone_num = synwit_ui_find_lv_obj(WIDGET_TEXTAREA_4);
    lv_textarea_set_text(ta_name,"");
    lv_textarea_set_text(ta_gender,"");
    lv_textarea_set_text(ta_age,"");
    lv_textarea_set_text(ta_phone_num,"");
    lv_obj_set_style_local_outline_pad(ta_name, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_outline_width(ta_name, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_outline_opa(ta_name, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, LV_OPA_TRANSP);
    lv_obj_set_style_local_outline_color(ta_name, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, LV_COLOR_RED);


    lv_obj_set_style_local_outline_pad(ta_gender, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_outline_width(ta_gender, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_outline_opa(ta_gender, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, LV_OPA_TRANSP);
    lv_obj_set_style_local_outline_color(ta_gender, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, LV_COLOR_RED);

    lv_obj_set_style_local_outline_pad(ta_age, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_outline_width(ta_age, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_outline_opa(ta_age, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, LV_OPA_TRANSP);
    lv_obj_set_style_local_outline_color(ta_age, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, LV_COLOR_RED);

    lv_obj_set_style_local_outline_pad(ta_phone_num, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_outline_width(ta_phone_num, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_outline_opa(ta_phone_num, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, LV_OPA_TRANSP);
    lv_obj_set_style_local_outline_color(ta_phone_num, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, LV_COLOR_RED);

    lv_obj_set_ext_click_area(ta_name, 50, 0, 50, 10);
    lv_obj_set_ext_click_area(ta_gender, 50, 40, 50, 0);
    lv_obj_set_ext_click_area(ta_age, 50, 0, 0, 50);
    lv_obj_set_ext_click_area(ta_phone_num, 50, 40, 0, 50);

    tc_area_next = synwit_ui_find_lv_obj(WIDGET_TOUCH_3);
    tc_area_skip = synwit_ui_find_lv_obj(WIDGET_TOUCH_4);
    lv_obj_set_event_cb(tc_area_next, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(tc_area_skip, my_event_cb);   /*Assign an event callback*/



    // 打开下面的注释可以为本界面开启一个每100ms触发一次的定时器
    screen_init();
    synwit_ui_start_scr_timer(10, NULL);
}

static void screen004_stop(synwit_id_t screen_id)
{
    /* 准备切换到其它界面前，这个接口会被调用。 */
    screen_stop();
}

static int screen004_favorite_stop_policy()
{
    /* 函数返回值表示本界面结束时，希望采用的资源管理策略 */

    /* 资源管理策略(主要影响本界面再次进入时的加载速度):
     *    0: 系统推荐，释放当前界面引用的资源，保留控件及控件状态。可获得平衡的界面切换性能和内存消耗
     *    1: 释放当前界面引用的资源，并删除所有控件。
     *    2: 保留当前界面的所有资源及控件。可以获得最佳的界面切换性能，但内存消耗较大
     */
    return 0;
}


/*
 * 界面注册对象
 */
synwit_screen_callback_t screen004_cb_obj = {
    .favorite_stop_policy = screen004_favorite_stop_policy,
    .on_screen_start = screen004_start,
    .on_screen_stop = screen004_stop,
    .on_screen_timer = screen004_on_timer,
};
uint16_t get_string_len(char *str)
{
    uint16_t len_res = 0;

    unsigned char  k, length;

    length = strlen(str); //取字符串总长
    
    for (k = 0; k < length; k++)
    {
        // printf("*(str + k) = %d\n",*(str + k));
        // printf("(uint8_t)*(str + k) = %d\n",(uint8_t)*(str + k));

        if ((uint8_t)*(str + k) <= 127)
        { //小于128是ASCII符号
        }
        else if ((uint8_t)*(str + k) > 127)
        { //大于127，为汉字，前后两个组成汉字内码
            k += 2; //汉字编码占3Byte,
        }
        len_res++;
    }

    return len_res;
}   
static void my_event_cb(lv_obj_t * obj, lv_event_t event)
{
    off_time_clr();
    if (obj == tc_area_next)
    {
        if (event == LV_EVENT_CLICKED)
        {
            //检查输入内容
            uint8_t flag_err = 0;
            char *p_string;
            uint16_t len = 0;
            e_gender_t gender_temp;

            p_string = (char *)lv_textarea_get_text(ta_name);
            len = get_string_len(p_string);
            printf("ta_name = %s len = %d\n",p_string,len);
            if (len == 0 || len > 15)
            {
                printf("name err\n");
                flag_err = 1;
                lv_obj_set_style_local_outline_pad(ta_name, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, 0);
                lv_obj_set_style_local_outline_width(ta_name, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, 2);
                lv_obj_set_style_local_outline_opa(ta_name, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, LV_OPA_COVER);
                lv_obj_set_style_local_outline_color(ta_name, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, LV_COLOR_RED);
                // lv_msgbox_show("请输入姓名", "姓名不能为空", NULL, true);
            }
            else
            {
                lv_obj_set_style_local_outline_pad(ta_name, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, 0);
                lv_obj_set_style_local_outline_width(ta_name, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, 0);
                lv_obj_set_style_local_outline_opa(ta_name, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, LV_OPA_TRANSP);
                lv_obj_set_style_local_outline_color(ta_name, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, LV_COLOR_RED);

            }

            p_string = (char *)lv_textarea_get_text(ta_gender);
            len = get_string_len(p_string);
            printf("ta_gender = %s len = %d\n",p_string,len);
            if (len == 0 || len != 1 || (strcmp(p_string, "男") != 0 && strcmp(p_string, "女") != 0))
            {
                printf("gender err\n");
                lv_obj_set_style_local_outline_pad(ta_gender, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, 0);
                lv_obj_set_style_local_outline_width(ta_gender, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, 2);
                lv_obj_set_style_local_outline_opa(ta_gender, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, LV_OPA_COVER);
                lv_obj_set_style_local_outline_color(ta_gender, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, LV_COLOR_RED);

                flag_err = 1;
                // lv_msgbox_show("请输入性别", "性别不能为空", NULL, true);
            }
            else
            {
                if (strcmp(p_string, "男") == 0)
                {
                    gender_temp = MAN;
                }
                else
                {
                    gender_temp = WOMAN;
                }
                
                lv_obj_set_style_local_outline_pad(ta_gender, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, 0);
                lv_obj_set_style_local_outline_width(ta_gender, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, 0);
                lv_obj_set_style_local_outline_opa(ta_gender, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, LV_OPA_TRANSP);
                lv_obj_set_style_local_outline_color(ta_gender, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, LV_COLOR_RED);

            }

            p_string = (char *)lv_textarea_get_text(ta_age);
            len = get_string_len(p_string);
            printf("ta_age = %s len = %d\n",p_string,len);
            if (len == 0 || len > 3)
            {
                printf("age err\n");
                flag_err = 1;
                lv_obj_set_style_local_outline_pad(ta_age, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, 0);
                lv_obj_set_style_local_outline_width(ta_age, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, 2);
                lv_obj_set_style_local_outline_opa(ta_age, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, LV_OPA_COVER);
                lv_obj_set_style_local_outline_color(ta_age, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, LV_COLOR_RED);

            }
            else
            {
                lv_obj_set_style_local_outline_pad(ta_age, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, 0);
                lv_obj_set_style_local_outline_width(ta_age, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, 0);
                lv_obj_set_style_local_outline_opa(ta_age, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, LV_OPA_TRANSP);
                lv_obj_set_style_local_outline_color(ta_age, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, LV_COLOR_RED);
            }

            p_string = (char *)lv_textarea_get_text(ta_phone_num);
            len = get_string_len(p_string);
            printf("ta_phone_num = %s len = %d\n",p_string,len);
            if (len == 0 || len > 11)
            {
                printf("phone err\n");
                flag_err = 1;
                lv_obj_set_style_local_outline_pad(ta_phone_num, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, 0);
                lv_obj_set_style_local_outline_width(ta_phone_num, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, 2);
                lv_obj_set_style_local_outline_opa(ta_phone_num, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, LV_OPA_COVER);
                lv_obj_set_style_local_outline_color(ta_phone_num, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, LV_COLOR_RED);

            }
            else
            {
                lv_obj_set_style_local_outline_pad(ta_phone_num, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, 0);
                lv_obj_set_style_local_outline_width(ta_phone_num, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, 0);
                lv_obj_set_style_local_outline_opa(ta_phone_num, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, LV_OPA_TRANSP);
                lv_obj_set_style_local_outline_color(ta_phone_num, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, LV_COLOR_RED);
            }


            if (flag_err == 0)
            {
                clr_mem_bank(g_e_user);
                g_s_mem_index = flash_get_mem_index_data(g_e_user,&g_e_flash_err,MAIN_ADDR);
                strcpy(( char *)g_s_mem_index.user_name_buf,(const char *)lv_textarea_get_text(ta_name));
                g_s_mem_index.gender = gender_temp;
                strcpy(( char *)g_s_mem_index.age_buf,(const char *)lv_textarea_get_text(ta_age));
                strcpy(( char *)g_s_mem_index.phone_num_buf,(const char *)lv_textarea_get_text(ta_phone_num));
                g_s_mem_index.flag_is_user_info_valid = 1;
                flash_save_mem_index_data(g_s_mem_index,g_e_user,MAIN_ADDR); //
                synwit_ui_load_screen(SCREEN003);
            }
        }
    }
    if (obj == tc_area_skip)
    {
        if (event == LV_EVENT_CLICKED)
        {
            synwit_ui_load_screen(SCREEN003);
        }
    }
}


